home *** CD-ROM | disk | FTP | other *** search
/ Mixa 155: Dogs / MIXA 155: Dogs.iso / pc / Viewer / BROWSER(W) / フィルタ / PDF / LIB / gs_cmap.ps < prev    next >
Encoding:
Text File  |  2002-10-29  |  11.0 KB  |  363 lines

  1. %    Copyright (C) 1995, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is licensed to a single customer by Artifex Software Inc.
  3. % under the terms of a specific OEM agreement.
  4.  
  5. % $RCSfile: gs_cmap.ps,v $ $Revision: 1.10 $
  6. % ProcSet for implementing CMap resources.
  7. % When this is run, systemdict is still writable.
  8.  
  9. % NOTE: Rearranged fonts are not implemented yet.
  10.  
  11. % ---------------- Public operators ---------------- %
  12.  
  13. % composefont doesn't appear in CMap files -- it's documented in
  14. % the "PostScript Language Reference Manual Supplement".
  15. /composefont {        % <name> <cmap|cmapname> <fonts> composefont <font>
  16.   10 dict begin
  17.     /CMap 2 index dup type /dicttype ne { /CMap findresource } if def
  18.     /FDepVector 1 index cvlit def    % temporarily
  19.     /Encoding [ 0 1 FDepVector length 1 sub { } for ] def
  20.     /FDepVector [ 0 1 FDepVector length 1 sub {
  21.         % Stack: name cmap[name] fonts /FDepVector [ fonts... i
  22.       FDepVector 1 index get
  23.       dup type /dicttype ne {
  24.     dup /CIDFont resourcestatus {
  25.       pop pop /CIDFont
  26.     } {
  27.       /Font
  28.     } ifelse findresource
  29.       } if
  30.       exch CMap /FontMatrices get dup length 2 index gt {
  31.     exch get dup null eq { pop } { makefont } ifelse
  32.       } {
  33.     pop pop
  34.       } ifelse
  35.     } for ] readonly def
  36.     /FMapType 9 def
  37.     /FontMatrix matrix def
  38.     /FontName 3 index def
  39.     CMap /WMode .knownget { /WMode exch def } if
  40.     /FontType 0 def
  41.   pop pop currentdict end /Font defineresource
  42. } bind odef
  43.  
  44. % ---------------- CMap operators ---------------- %
  45.  
  46. 40 dict begin
  47.  
  48. % Our internal .CodeMapData structure closely mirrors the structures
  49. % defined in gxfcmap.h (q.v.).  () indicate a string, [] indicate an array,
  50. % ? indicates a Boolean, # indicates an integer, {} for grouping.
  51. %    [[(first) (last) ...]        % code space ranges
  52. %     [(prefix) (key_size,?is_range,value_type,value_size) (keys...)
  53. %       {(values...) | [value ...]} #font_index    % code mappings
  54. %      ...]
  55. %     <<same>>        % notdef mappings
  56. %    ]
  57. % FontMatrices is the array of matrices defined by begin/endusematrix.
  58. % All of the arrays and strings are read-only after they have been built.
  59. %
  60. % Note that the code in zfcmap.c that constructs the C structures from
  61. % the PostScript structures has intimate knowledge of the above format.
  62.  
  63. % ****** NOTE: The code currently only handles "well-behaved" CMaps:
  64. %    - CID values only (no bfchars), 16-bit
  65. %    - Entries (both code space and map) must be sorted
  66. %    - Only the last byte must vary in each map range, except for
  67. %    the identity mapping
  68.  
  69. % ------ Font-level operators ------ %
  70.  
  71. /begincmap {        % - begincmap -
  72.   /.CodeMapData [[] [] []] def
  73.   /FontMatrices [] def
  74.   /.FontIndex 0 def
  75.   /.TempMaps [20 dict 50 dict 50 dict] def
  76.   /CodeMap null def        % for .buildcmap
  77. } bind def
  78. /endcmap {        % - endcmap -
  79.   10 dict begin 0 1 2 {
  80.     /i exch def
  81.         % Append data from .TempMaps to .CodeMapData.
  82.     /t .TempMaps i get def
  83.     .CodeMapData i get length t { exch pop length add } forall
  84.     array /a exch def
  85.     a 0 .CodeMapData i get .putmore
  86.     0 1 t length 1 sub {
  87.       t exch get .putmore
  88.     } for pop pop
  89.     .CodeMapData i a put
  90.   } for end
  91.   currentdict /.TempMaps undef
  92.   /.CodeMapData .CodeMapData .endmap def
  93.   /FontMatrices FontMatrices .endmap def
  94. } bind def
  95.  
  96. /.putmore {        % <array> <i> <array2> .putmore <array> <i+len(array2)>
  97.   3 copy putinterval length add
  98. } bind def
  99.  
  100. /.endmap {        % <map> .endmap <map>
  101.   dup type /arraytype eq {
  102.     % This might be a shared read-only array inherited via usecmap.
  103.     % Don't try to update its elements if this is the case.
  104.     dup wcheck {
  105.       0 1 2 index length 1 sub {
  106.     2 copy 2 copy get .endmap put pop
  107.       } for readonly
  108.     } if
  109.   } {
  110.     dup type /stringtype eq { readonly } if
  111.   } ifelse
  112. } bind def
  113.  
  114. /.appendmap {        % -mark- <elt> ... <array#> .appendmap -
  115.   .TempMaps exch get counttomark 1 add 1 roll
  116.   ] 1 index length exch put
  117. } bind def
  118.  
  119. /begincodespacerange {    % <count> begincodespacerange -
  120.   pop mark
  121. } bind def
  122. /endcodespacerange {    % <code_lo> <code_hi> ... endcodespacerange -
  123.   0 .appendmap
  124. } bind def
  125.  
  126. /usecmap {        % <CMap_name> usecmap -
  127.   /CMap findresource dup
  128.         % Copy the top level of .CodeMapData
  129.   /.CodeMapData exch /.CodeMapData get copyarray def
  130.   /FontMatrices exch /FontMatrices get copyarray def
  131. } bind def
  132.  
  133. /usefont {        % <fontID> usefont -
  134.   /.FontIndex exch def
  135. } bind def
  136.  
  137. /beginusematrix {    % <fontID> beginusematrix -
  138.   FontMatrices wcheck not FontMatrices length 2 index le or {
  139.     FontMatrices length 1 index 1 add .max array
  140.     dup 0 FontMatrices putinterval
  141.     /FontMatrices exch def
  142.   } if
  143. } bind def
  144. /endusematrix {        % <matrix> endusematrix -
  145.   FontMatrices 3 1 roll put
  146. } bind def
  147.  
  148. % ------ Rearranged font operators ------ %
  149.  
  150. /beginrearrangedfont {    % <font_name> <font*> beginrearrangedfont -
  151.   10 dict begin
  152.   /.FontNames exch def
  153.   /.FontName exch def
  154.   begincmap
  155. } bind def
  156. /endrearrangedfont {    % - endrearrangedfont -
  157.   (REARRANGED FONTS NOT IMPLEMENTED YET.) = flush
  158.   FontName .FontNames 0 get findfont end definefont pop
  159. } bind def
  160.  
  161. % ------ Character name/code selector operators ------ %
  162.  
  163. /beginbfchar {        % <count> beginbfchar -
  164.   pop mark
  165. } bind def
  166. /endbfchar {        % <code> <to_code|charname> ... endbfchar
  167.   counttomark 2 idiv {
  168.     counttomark -2 roll        % process in correct order
  169.     .addbfchar
  170.   } repeat 1 .appendmap
  171. } bind def
  172.  
  173. /beginbfrange {        % <count> beginbfrange -
  174.   pop mark
  175. } bind def
  176. /endbfrange {        % <code_lo> <code_hi> <to_code|(charname*)> ...
  177.             %   endbfrange -
  178.   counttomark 3 idiv {
  179.     counttomark -3 roll        % process in correct order
  180.     dup type dup /arraytype eq exch /packedarraytype eq or {
  181.             % Array value, split up.
  182.       exch pop {
  183.             % Stack: code to_code|charname
  184.     1 index exch .addbfchar
  185.             % Increment the code.  As noted above, we require
  186.             % that only the last byte vary, but we still must
  187.             % mask it after incrementing, in case the last
  188.             % value was 0xff.
  189.             % Stack: code prefix params key value fontindex
  190.     6 -1 roll dup length string copy
  191.     dup dup length 1 sub 2 copy get 1 add 255 and put
  192.       } forall pop
  193.     } {
  194.             % Single value, handle directly.
  195.       .addbfrange
  196.     } ifelse
  197.   } repeat 1 .appendmap
  198. } bind def
  199.  
  200. /.addbfchar {        % <code> <to_code|charname> .addbfchar
  201.             %   <prefix> <params> <key> <value> <font_index>
  202.   1 index exch .addbfrange
  203. } bind def
  204. /.addbfrange {        % <code_lo> <code_hi> <to_code|charname>
  205.             %   .addbfrange <<same as .addbfchar>>
  206.   4 string dup 3
  207.   3 index type /nametype eq {
  208.     2 index 2 1 put
  209.     4 -1 roll 1 array astore 4 1 roll 4
  210.   } {
  211.     2 index 2 2 put
  212.     3 index length
  213.   } ifelse put
  214.             % Stack: code_lo code_hi value params
  215.   3 index 3 index eq {
  216.             % Single value.
  217.     3 -1 roll pop exch () exch
  218.   } {
  219.             % Range.
  220.     dup 0 1 put dup 1 1 put
  221.     4 2 roll
  222.     dup dup length 1 sub 0 exch getinterval 5 1 roll    % prefix
  223.             % Stack: prefix value params code_lo code_hi
  224.     2 { exch dup length 1 sub 1 getinterval } repeat concatstrings
  225.     3 -1 roll
  226.   } ifelse
  227.   .FontIndex
  228. } bind def
  229.  
  230. % ------ CID selector operators ------ %
  231.  
  232. /begincidchar {        % <count> begincidchar -
  233.   pop mark
  234. } bind def
  235. /endcidchar {        % <code> <cid> ... endcidchar -
  236.   1 .endmapchars
  237. } bind def
  238.  
  239. /begincidrange {    % <count> begincidrange -
  240.   pop mark
  241. } bind def
  242. /endcidrange {        % <code_lo> <code_hi> <cid_base> ... endcidrange -
  243.   1 .endmapranges
  244. } bind def
  245.  
  246. /.endmapchars {        % -mark- <code> <cid> ... <map#> .endmapchars -
  247.   counttomark 1 add 1 roll
  248.   counttomark 2 idiv {
  249.     counttomark -2 roll        % process in correct order
  250.         % Construct prefix, params, key, value, font_index
  251.     <00 00 00 02> ()    % params, key
  252.     3 -1 roll .endmapvalue
  253.   } repeat
  254.   counttomark 2 add -1 roll .appendmap
  255. } bind def
  256.  
  257. /.endmapranges {    % -mark- <code_lo> <code_hi> <cid_base> ... <map#>
  258.             %   .endmapranges -
  259.   counttomark 1 add 1 roll
  260.   counttomark 3 idiv {
  261.     counttomark -3 roll        % process in correct order
  262.         % Construct prefix, params, key_lo, key_hi, value, font_index
  263.     3 1 roll dup length 1 eq {
  264.       () 3 1 roll    % prefix
  265.       <01 01 00 02>    % params
  266.       3 1 roll        % keys
  267.       concatstrings 4 -1 roll .endmapvalue
  268.     } {
  269.         % Stack: cid_base code_lo code_hi
  270.         % Hack: handle 16-bit single-range mappings specially.
  271.       counttomark 3 eq 1 index length 2 eq and {
  272.     () 3 1 roll    % prefix
  273.     <02 01 00 02>    % params
  274.     3 1 roll    % keys
  275.     concatstrings 4 -1 roll .endmapvalue
  276.       } {
  277.     exch dup dup length 1 sub 0 exch getinterval    % prefix
  278.             % Stack: cid_base code_hi code_lo prefix
  279.     <01 01 00 02>    % params
  280.     3 -1 roll dup length 1 sub 1 getinterval    % key_lo
  281.     4 -1 roll dup length 1 sub 1 getinterval    % key_hi
  282.     concatstrings
  283.     4 -1 roll .endmapvalue
  284.         % See if we can merge with the previous value.
  285.         % The prefix, params, and font index must match.
  286.         % Stack: prefix params keys value fontindex
  287.     4 index 10 index eq        % prefix
  288.     4 index 10 index eq and    % params
  289.     1 index 7 index eq and    % fontindex
  290.     {
  291.       pop 4 2 roll pop pop
  292.         % Stack: prefix params keys value fontindex keys2 value2
  293.       5 -1 roll 3 -1 roll concatstrings
  294.         % Stack: prefix params value fontindex value2 keys'
  295.       4 -1 roll 3 -1 roll concatstrings
  296.         % Stack: prefix params fontindex keys' values'
  297.       3 -1 roll
  298.     } if
  299.       } ifelse
  300.     } ifelse
  301.   } repeat
  302.   counttomark 2 add -1 roll .appendmap
  303. } bind def
  304.  
  305. /.endmapvalue {        % <cid> .endmapvalue (hi,lo) .FontIndex
  306.   2 string dup 0 3 index -8 bitshift put    % value
  307.   dup 1 4 -1 roll 255 and put
  308.   .FontIndex        % font_index
  309. } bind def
  310.  
  311. % ------ notdef operators ------ %
  312.  
  313. /beginnotdefchar {    % <count> beginnotdefchar -
  314.   pop mark
  315. } bind def
  316. /endnotdefchar {    % <code> <cid> ... endnotdefchar -
  317.   2 .endmapchars
  318. } bind def
  319.  
  320. /beginnotdefrange {    % <count> beginnotdefrange -
  321.   pop mark
  322. } bind def
  323. /endnotdefrange {    % <code_lo> <code_hi> <cid> ... endnotdefrange -
  324.   2 .endmapranges
  325. } bind def
  326.  
  327. % ---------------- Resource category definition ---------------- %
  328.  
  329. currentdict end
  330.  
  331. languagelevel exch 2 .setlanguagelevel
  332.  
  333. /CMap /Generic /Category findresource dup length dict .copydict
  334. dup /InstanceType /dicttype put
  335. dup /DefineResource {
  336.         % The AdobePS5 Windows driver emits code that attempts to
  337.         % create CMaps without the required CMapName entry.
  338.         % Work around this here.
  339.   dup /CMapName known not {
  340.     dup wcheck not {
  341.       .currentglobal exch dup wcheck .setglobal
  342.       dup length dict .copydict exch .setglobal
  343.     } if
  344.     dup gcheck 2 index gcheck not and {
  345.       exch .currentglobal exch true .setglobal
  346.       dup length string copy exch .setglobal exch
  347.     } if dup /CMapName 3 index put
  348.   } if
  349.   dup /CodeMap get null eq { .buildcmap } if
  350.   /Generic /Category findresource /DefineResource get exec
  351. } put
  352. /Category defineresource pop
  353.     % We might have loaded CID font support already.
  354. /CIDInit /ProcSet 2 copy { findresource } .internalstopped
  355.     % An interior `stopped' might have reset VM allocation to local.
  356. true .setglobal
  357.  { pop pop 3 -1 roll }
  358.  { dup length 4 index length add dict .copydict 4 -1 roll exch .copydict }
  359. ifelse exch defineresource pop
  360.  
  361. .setlanguagelevel
  362.